package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.House;
import com.atguigu.entity.HouseImage;
import com.atguigu.entity.bo.HouseQueryBo;
import com.atguigu.entity.vo.HouseVo;
import com.atguigu.mapper.DictMapper;
import com.atguigu.mapper.HouseImageMapper;
import com.atguigu.mapper.HouseMapper;
import com.atguigu.service.HouseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

@Service
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private HouseImageMapper houseImageMapper;
    @Override
    public BaseMapper<House> getBaseMapper() {
        return houseMapper;
    }

    @Override
    public void publish(Long id, Integer status) {
        House house = new House();
        house.setId(id);
        house.setStatus(status);
        houseMapper.update(house);
    }


    @Override
    public House getById(Long id) {
        House house = houseMapper.getById(id);
        house.setHouseTypeName(dictMapper.getNameById(house.getHouseTypeId()));
        house.setFloorName(dictMapper.getNameById(house.getFloorId()));
        house.setBuildStructureName(dictMapper.getNameById(house.getBuildStructureId()));
        house.setDirectionName(dictMapper.getNameById(house.getDirectionId()));
        house.setDecorationName(dictMapper.getNameById(house.getDecorationId()));
        house.setHouseUseName(dictMapper.getNameById(house.getHouseUseId()));
        return house;
    }

    @Override
    public PageInfo<HouseVo> findVoListPage(Integer pageNum, Integer pageSize, HouseQueryBo houseQueryBo) {
        PageHelper.startPage(pageNum, pageSize);
        Page<HouseVo> page = houseMapper.findVoListPage(houseQueryBo);
        for (HouseVo houseVo : page) {
            Long houseTypeId = houseVo.getHouseTypeId();
            houseVo.setHouseTypeName(dictMapper.getNameById(houseTypeId));
            Long floorId = houseVo.getFloorId();
            houseVo.setFloorName(dictMapper.getNameById(floorId));
            Long directionId = houseVo.getDirectionId();
            houseVo.setDirectionName(dictMapper.getNameById(directionId));
            //设置第一张房源照片位默认照片
            if (StringUtils.isEmpty(houseVo.getDefaultImageUrl())) {
                HouseImage houseImage = houseImageMapper.findHouseImageList(houseVo.getId(), 1).get(0);
                String imageUrl = houseImage.getImageUrl();
                houseVo.setDefaultImageUrl(imageUrl);
            }
        }
        return new PageInfo<>(page);
    }
}